<template>
  <div>更换头衔</div>
  <el-upload
    class="avatar-uploader"
    :show-file-list="false"
    :auto-upload="false"
    :on-change="onSelectFile"
  >
    <img v-if="userData.user_pic" :src="userData.user_pic" class="avatar" />
    <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
  </el-upload>
</template>
  
<script setup>
import { ref, onMounted } from 'vue'
import { getUserInfo, updateUserInfo } from '@/api/UserInfo.js'
import { ElMessage } from 'element-plus'

const id = JSON.parse(localStorage.getItem('userInfo'))?.id
const userData = ref({})

onMounted(() => {
  getUserInfoFun()
})

const onSelectFile = (file) => {
  userData.value.user_pic = URL.createObjectURL(file.raw)
  modify()
}

// 获取用户基本信息
const getUserInfoFun = async () => {
  const res = await getUserInfo(id)
  if (res.status === 0) {
    userData.value = res.data
  }
}

// 更新用户的基本信息
const modify = async () => {
  const res = await updateUserInfo(userData.value)
  if (res.status === 0) {
    ElMessage({
      message: res.message,
      type: 'success'
    })
  }
}
</script>
  
<style scoped lang="scss">
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
  border: 1px dashed #8c939d;
}
</style>
  